from datetime import datetime
from textual.widgets import Digits, Static
from textual.containers import Horizontal, Vertical
import psutil


class Clock(Digits):

    def on_mount(self) -> None:
        self.set_interval(1, self.update_clock_time, pause=False)
    
    def update_clock_time(self):
        clock = datetime.now().time()
        self.query_one("#clock").update(f"{clock:%T}")
        date = datetime.now().date()
        self.query_one("#date").update(f"{date:%Y年%m月%d日}")
    
    def compose(self):
        yield Horizontal(
                Vertical(
                    Horizontal(Static(f"{datetime.now().date():%Y年%m月%d日}", id="date"),
                               id="date_container"),
                    Horizontal(Digits("", id="clock"),
                               id="clock_container"),
                    Horizontal(Static("系统启动: "+str(datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")), id="boot_time"),id="boot_time_container"),
                id="four_container",
                ),
            )